package com.yuxer.mybatisplus.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * mybatisPlus 创建数据时默认更改/添加项
 *
 * @author 醋醋🐟
 * @since 2021/4/8 21:42
 */

@Configuration
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        // 创建时间
        this.strictInsertFill(metaObject, "addDateTime", LocalDateTime.class, LocalDateTime.now());
        // 创建日期
        this.strictInsertFill(metaObject, "addDate", LocalDate.class, LocalDate.now());
        // 入职时间
        this.strictInsertFill(metaObject, "joinDate", LocalDate.class, LocalDate.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updDateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updDate", LocalDate.now(), metaObject);
    }

}